package com.universal.schedule.fragments;

import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.HapticFeedbackConstants;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ImageButton;
import android.widget.ListView;

import com.melnykov.fab.FloatingActionButton;
import com.universal.schedule.AlarmPreferencesActivity;
import com.universal.schedule.R;
import com.universal.schedule.adapter.AlarmListAdapter;
import com.universal.schedule.db.DatabaseHelper;
import com.universal.schedule.enity.Alarm;
import com.universal.schedule.service.AlarmServiceBroadcastReciever;

import java.util.List;


/**
 *
 */
public class AlarmsFragments extends Fragment {
	private View layout;
	private FloatingActionButton mImageAdd;
	ImageButton newButton;
	ListView mathAlarmListView;
	AlarmListAdapter alarmListAdapter;
	public AlarmsFragments() {
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		if (layout == null) {
			layout = inflater.inflate(R.layout.fragments_alarms, container, false);
			initUI();
			setListener();
		}
		return layout;
	}

	private void setListener() {
		mImageAdd.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent newAlarmIntent = new Intent(getActivity(), AlarmPreferencesActivity.class);
				startActivity(newAlarmIntent);
				
			}
		});
	}

	private void initUI() {
		mImageAdd = (FloatingActionButton) layout.findViewById(R.id.fab);
		
		mathAlarmListView = (ListView)layout.findViewById(android.R.id.list);
		mathAlarmListView.setLongClickable(true);
		mathAlarmListView.setOnItemLongClickListener(new OnItemLongClickListener() {
			@Override
			public boolean onItemLongClick(AdapterView<?> adapterView, View view, int position, long id) {
				view.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
				final Alarm alarm = (Alarm) alarmListAdapter.getItem(position);
				Builder dialog = new Builder(getActivity());
				dialog.setTitle("Delete");
				dialog.setMessage("Delete this alarm?");
				dialog.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {

						DatabaseHelper.deleteEntry(alarm);
						callMathAlarmScheduleService();
						
						updateAlarmList();
					}
				});
				dialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
					}
				});

				dialog.show();

				return true;
			}
		});

		callMathAlarmScheduleService();

		alarmListAdapter = new AlarmListAdapter(getActivity());
		this.mathAlarmListView.setAdapter(alarmListAdapter);
		mathAlarmListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View v, int position, long id) {
				v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
				Alarm alarm = (Alarm) alarmListAdapter.getItem(position);
				Intent intent = new Intent(getActivity(), AlarmPreferencesActivity.class);
				intent.putExtra("alarm", alarm);
				startActivity(intent);
			}

		});
		
	}
	@Override
	public void onResume() {
		super.onResume();
		updateAlarmList();
	}
	@Override
	public void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		DatabaseHelper.deactivate();
	}
	
	public void updateAlarmList(){
		final List<Alarm> alarms = DatabaseHelper.getAll();
		alarmListAdapter.setMathAlarms(alarms);
		
		getActivity().runOnUiThread(new Runnable() {
			public void run() {
				// reload content			
				alarmListAdapter.notifyDataSetChanged();				
				if(alarms.size() > 0){
//					getActivity().findViewById(android.R.id.empty).setVisibility(View.INVISIBLE);
					getActivity().findViewById(R.id.image_hello).setVisibility(View.INVISIBLE);
					
				}else{
//					getActivity().findViewById(android.R.id.empty).setVisibility(View.VISIBLE);
					getActivity().findViewById(R.id.image_hello).setVisibility(View.VISIBLE);
				}
			}
		});
	}
	
	protected void callMathAlarmScheduleService() {
		Intent mathAlarmServiceIntent = new Intent(getActivity(), AlarmServiceBroadcastReciever.class);
		getActivity().sendBroadcast(mathAlarmServiceIntent, null);
	}

}
